package com.cby.android.sample

import android.os.Build
import android.os.Bundle
import androidx.viewpager.widget.ViewPager
import com.cby.android.library.adapter.CommonViewPagerAdapter
import com.cby.android.library.base.BaseActivity
import com.cby.android.library.base.BlankFragment
import com.cby.android.library.ext.observe
import com.cby.android.sample.constant.Constant
import com.cby.android.sample.ext.initLocationAboveM
import com.cby.android.sample.ext.initLocationLowM
import com.cby.android.sample.ext.requestPermissionsResult
import com.cby.android.sample.fragments.*
import com.google.android.material.tabs.TabLayout

class MainActivity : BaseActivity() {

    private lateinit var mainPager: ViewPager
    private lateinit var bottomLayout: TabLayout

    var countryName: String? = null
    var provinceName: String? = null
    var cityName: String? = null

    //纬度
    var latitude: Double = 0.0

    //经度
    var longitude: Double = 0.0

    val locationCodeReq = 1001

    override fun getLayoutId(): Int = R.layout.activity_main

    override fun onViewInit() {
        mainPager = findViewById(R.id.vp_main_container)
        mainPager.offscreenPageLimit = 5
        bottomLayout = findViewById(R.id.bottom_nav)

        mainPager.addOnPageChangeListener(
            TabLayout.TabLayoutOnPageChangeListener(bottomLayout)
        )

        MainBottomNav.setBottomTabs(bottomLayout, layoutInflater)
        bottomLayout.addOnTabSelectedListener(
            object : TabLayout.ViewPagerOnTabSelectedListener(mainPager) {
                override fun onTabSelected(tab: TabLayout.Tab) {
                    mainPager.currentItem = tab.position
                }

                override fun onTabReselected(tab: TabLayout.Tab?) {
                }
            })

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            initLocationAboveM()
        } else {
            initLocationLowM()
        }
    }

    override fun onDataInit() {
        val pagerAdapter =
            CommonViewPagerAdapter(
                supportFragmentManager
            )
        val homeFragment = HomeFragment()
        val location = "$longitude,$latitude"
        val data = Bundle()
        data.putString(Constant.KEY_LOCATION, location)
        homeFragment.arguments = data
        pagerAdapter.addFragment(homeFragment, getString(R.string.wan_home_title))
        pagerAdapter.addFragment(RollNewsFragment(), getString(R.string.wan_project_title))
        pagerAdapter.addFragment(SquareFragment(), getString(R.string.wan_square_title))
        pagerAdapter.addFragment(TxWxNewFragment(), getString(R.string.wan_public_title))
        pagerAdapter.addFragment(BlankFragment(), getString(R.string.wan_my_title))

        mainPager.adapter = pagerAdapter
        mainPager.offscreenPageLimit = 5
        mainPager.currentItem = 0
    }

    override fun onRequestPermissionsResult(
        requestCode: Int,
        permissions: Array<out String>,
        grantResults: IntArray
    ) {
        requestPermissionsResult(requestCode,permissions, grantResults)
        super.onRequestPermissionsResult(requestCode, permissions, grantResults)
    }
}